﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：BLL_HospitalProcureCatalog.cs
    // 文件功能描述：医疗机构采购目录操作业务逻辑类,,实现接口IBLL_Company,继承自类MarshalByRefObject
    // 创建标识：周司徒2009-03-22
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using HZ.DrugTrade.DAL;
using HZ.DrugTrade.IBLL;
using HZ.Common;
using HZ.DrugTrade.Model;

namespace HZ.DrugTrade.BLL
{
    /// <summary>
    /// 医疗机构采购目录操作业务逻辑类,实现接口IBLL_Company,继承自类MarshalByRefObject
    /// </summary>
    public class BLL_HospitalProcureCatalog : MarshalByRefObject, IBLL_HospitalProcureCatalog
    {
        #region IHospitalProcureCatalog Members

        /// <summary>
        /// 增加医疗机构采购目录
        /// </summary>
        /// <param name="hospitalprocurecatalog"></param>
        /// <returns></returns>
        public int AddHospitalProcureCatalog(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            BLL_UserSession.CheckSession();
            int state = DAL.DAL_HospitalProcureCatalog.AddHospitalProcureCatalog(hospitalprocurecatalog);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("增加医疗机构采购目录：{0};处理时间:{1};)", hospitalprocurecatalog.ProcureCatalogID, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 医疗机构增加一个采购目录(批量)
        /// </summary>
        /// <param name="hospitalprocurecatalogList">医疗机构采购目录(医疗机构编号，采购目录编号必填)</param>
        public int AddHospitalProcureCatalog_Batch(IList<HZ.DrugTrade.Model.HospitalProcureCatalog> hospitalprocurecatalogList)
        {
            BLL_UserSession.CheckSession();
            int result =  DAL.DAL_HospitalProcureCatalog.AddHospitalProcureCatalog_Batch(hospitalprocurecatalogList);
            string strlenth = "";
            if (result > 0) {
                for (int i = 0; i < hospitalprocurecatalogList.Count;i++ ) {
                  strlenth = strlenth+ hospitalprocurecatalogList[i].ProcureCatalogID + "-";
                }
                BLL_Log.Instance.AddKeyOperationLog(new Model.KeyOperationLog() { OperateMessage = string.Format("医疗机构批量添加采购目录{0}",strlenth ) }); 
            } 
            return result;
        }
        

        /// <summary>
        /// 更新医疗机构采购目录
        /// </summary>
        /// <param name="hospitalprocurecatalog"></param>
        /// <returns></returns>
        public int UpdateHospitalProcureCatalog(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            BLL_UserSession.CheckSession();
            int state = DAL.DAL_HospitalProcureCatalog.UpdateHospitalProcureCatalog(hospitalprocurecatalog);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("更新医疗机构采购目录：{0};处理时间:{1};)", hospitalprocurecatalog.ProcureCatalogID, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }

        /// <summary>
        /// 获取不在医疗机构采购目录中的系统采购目录
        /// </summary>
        /// <param name="query"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.ProcureCatalog> GetProcureCatalogNotInCurrentCatalog(HZ.DrugTrade.Model.Query.HospitalProcureCatalog query, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            HZ.Common.ThrowHelper.Throw(query.HospitalID != null, "query.HospitalID != null");
            HZ.DrugTrade.Model.Query.ProcureCatalog pcatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            string append = string.Format(" ProcureCatalogID NOT IN (SELECT ProcureCatalogID FROM TB_HospitalProcureCatalog WHERE HospitalID = '{0}') ", query.HospitalID);
            if (string.IsNullOrEmpty(query.Append))
            {
                query.Append = append;

            }
            else
            {
                query.Append += " AND " + append; ;
            }
            pcatalog.Append = query.Append;
            pcatalog.SortFields = query.SortFields;
            pcatalog.Outlookc = query.Outlookc;
            pcatalog.CompanyName_SC = query.Companyname_Sc;
            pcatalog.IsUsing = query.IsUsing;
            if (query.ProjectID != null)
            {
                pcatalog.ProjectID = query.ProjectID;
            }
            pcatalog.PinyinCode = query.PinyinCode;
            pcatalog.SortName = query.SortName;
            pcatalog.GoodsID = query.GoodsID;
            pcatalog.PackUnit = query.PackUnit;
            pcatalog.QualityLevel = query.QualityLevel;
            //pcatalog.PinyinCode = query.PinyinCode;  //update by lhl 2009-08-26
            //pcatalog.CompanyName_TB = query.Companyname_Tb;//added by lig 2009-8-27 
            pcatalog.GoodsName = query.GoodsName;//added by lig 2009-9-30
            pcatalog.Material = query.Material;
            pcatalog.Unit = query.Unit;//add by ml 2011-01-17

            pcatalog.SortSource = query.SortSource.ToString();//add by fc 2010-04-28
            BLL.BLL_ProcurementBasic pbCtrl = new BLL_ProcurementBasic();

            return pbCtrl.GetProcureCatalogByWhere(pcatalog, currentpage, pagesize, out totalrecord);
            throw new NotImplementedException();
            // return GetHospitalProcureCatalog(query, currentpage, pagesize, out totalrecord);
        }


        /// <summary>
        /// 获取医疗机构采购目录
        /// </summary>
        /// <param name="query">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> GetHospitalProcureCatalog(HZ.DrugTrade.Model.Query.HospitalProcureCatalog query, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetHospitalProcureCatalog(query, currentpage, pagesize, out totalrecord);
        }

        /// <summary>
        /// 获取采购目录
        /// </summary>
        /// <param name="query">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.DrugTrade.Model.View.HospitalProcureCatalog> GetAreaProductByWhere(HZ.DrugTrade.Model.Query.HospitalProcureCatalog query, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetAreaProductByWhere(query, currentpage, pagesize, out totalrecord);
        }


        /// <summary>
        /// 撤废医疗机构采购目录
        /// </summary>
        /// <param name="hospitalprocurecatalog">医疗机构采购目录(医疗机构编号，采购目录编号必填)</param>
        public int InvalidateHospitalProcureCatalog(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            BLL_UserSession.CheckSession();
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog != null);
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.ProcureCatalogID != null);
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.HospitalID != null);
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.ProcureCatalogID), "ProcureCatalogID不能空");
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.HospitalID), "HospitalID不能空");
            Model.HospitalProcureCatalog updatemodel = HZ.Data.DAL_Helper.GetModelByID(hospitalprocurecatalog);
            updatemodel.ProcureCatalogID = hospitalprocurecatalog.ProcureCatalogID;
            updatemodel.HospitalID = hospitalprocurecatalog.HospitalID;
            updatemodel.IsUsing = false;
            int result = UpdateHospitalProcureCatalog(updatemodel);
            if (result > 0) {
                BLL_Log.Instance.AddKeyOperationLog(new Model.KeyOperationLog() { OperateMessage = string.Format("废弃医疗机构采购目录{0}", updatemodel) }); 
            }
            return result;
        }

        /// <summary>
        /// 恢复医疗机构采购目录
        /// </summary>
        /// <param name="hospitalprocurecatalog">医疗机构采购目录(医疗机构编号，采购目录编号必填)</param>
        public int ValidateHospitalProcureCatalog(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            BLL_UserSession.CheckSession();
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog != null);
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.ProcureCatalogID != null);
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.HospitalID != null);
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.ProcureCatalogID), "ProcureCatalogID不能空");
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.HospitalID), "HospitalID不能空");
            Model.HospitalProcureCatalog updatemodel = HZ.Data.DAL_Helper.GetModelByID(hospitalprocurecatalog);
            updatemodel.ProcureCatalogID = hospitalprocurecatalog.ProcureCatalogID;
            updatemodel.HospitalID = hospitalprocurecatalog.HospitalID;
            updatemodel.IsUsing = true;
  
            int result = UpdateHospitalProcureCatalog(updatemodel);
            if (result > 0)
            {
                BLL_Log.Instance.AddKeyOperationLog(new Model.KeyOperationLog() { OperateMessage = string.Format("恢复医疗机构采购目录{0}", updatemodel) });
            }
            return result;
        }

        /// <summary>
        /// 撤废医疗机构采购目录(批量)
        /// </summary>
        /// <param name="hospitalprocurecatalogList">医疗机构采购目录(医疗机构编号，采购目录编号必填)</param>
        public int InvalidateHospitalProcureCatalog_Batch(IList<HZ.DrugTrade.Model.HospitalProcureCatalog> hospitalprocurecatalogList)
        {
             
            BLL_UserSession.CheckSession();
            int result = DAL.DAL_HospitalProcureCatalog.ValidateHospitalProcureCatalog_Batch(hospitalprocurecatalogList, 0);
            string srtlenth="";
            for (int i = 0; hospitalprocurecatalogList.Count > i;i++ )
            {
              srtlenth = srtlenth+ hospitalprocurecatalogList[i]+"-";
            }
            BLL_Log.Instance.AddKeyOperationLog(new Model.KeyOperationLog() { OperateMessage = string.Format("撤废医疗机构采购目录{0}", srtlenth) });
            return result;
        }

        /// <summary>
        /// 恢复医疗机构采购目录(批量)
        /// </summary>
        /// <param name="hospitalprocurecatalogList">医疗机构采购目录(医疗机构编号，采购目录编号必填)</param>
        public int ValidateHospitalProcureCatalog_Batch(IList<HZ.DrugTrade.Model.HospitalProcureCatalog> hospitalprocurecatalogList)
        {
            BLL_UserSession.CheckSession();
            int result = DAL.DAL_HospitalProcureCatalog.ValidateHospitalProcureCatalog_Batch(hospitalprocurecatalogList, 1);
            string srtlenth = "";
            for (int i = 0; hospitalprocurecatalogList.Count > i; i++)
            {
                srtlenth = srtlenth + hospitalprocurecatalogList[i] + "-";
            }
            BLL_Log.Instance.AddKeyOperationLog(new Model.KeyOperationLog() { OperateMessage = string.Format("恢复医疗机构采购目录{0}", srtlenth) });
            return result;
          
        }

        /// <summary>
        /// 更新医疗机构采购产品零售价
        /// </summary>
        /// <param name="hospitalprocurecatalog"></param>
        /// <returns></returns>
        public int UpdateHospitalProcureCatalogSellPrice(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog != null);
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.HospitalID), "医疗机构编号HospitalID不能为空");
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.ProcureCatalogID), "采购目录编号ProcureCatalogID不能空");
            HZ.Common.ThrowHelper.Throw( hospitalprocurecatalog.SellPrice!=null , "医疗机构零售价SellPrice不能空");
            int state = HZ.Data.DAL_Helper.OperateTableSQL(hospitalprocurecatalog, HZ.Data.TableOperator.Update);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("更新医疗机构采购产品零售价：{0};处理时间:{1};)", hospitalprocurecatalog.ProcureCatalogID, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 更改医疗机构产品的配送企业，及实际采购价格
        /// </summary>
        /// <param name="hospitalprocurecatalog"></param>
        /// <returns></returns>
        public int UpdateHospitalProcureCatalogCompanyPS(HZ.DrugTrade.Model.HospitalProcureCatalog hospitalprocurecatalog)
        {
            HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog != null);
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.HospitalID), "医疗机构编号HospitalID不能为空");
            HZ.Common.ThrowHelper.Throw(!string.IsNullOrEmpty(hospitalprocurecatalog.ProcureCatalogID), "采购目录编号ProcureCatalogID不能空");
            //HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.CompanyID_PS != null, "配送企业不能为空");
            //HZ.Common.ThrowHelper.Throw(hospitalprocurecatalog.Price != null, "配送企业不能为空");
            //修改配送关系的日志
            //BLL_Log.Instance.AddKeyOperationLog(new HZ.DrugTrade.Model.KeyOperationLog() { OperateMessage = string.Format(HZ.Data.DAL_Helper.GetUpdateSql(hospitalprocurecatalog)) });
           // HZ.Data.DAL_Helper.OperateTableSQL(hospitalprocurecatalog, HZ.Data.TableOperator.Update);
            int state = HZ.Data.DAL_Helper.OperateTableSQL(hospitalprocurecatalog, HZ.Data.TableOperator.Update);
            if(state == 1)
            {
                BLL_Log.Instance.AddKeyOperationLog(new KeyOperationLog { OperateMessage = string.Format("更改医疗机构产品的配送企业，及采购价：{0};处理时间:{1};)", hospitalprocurecatalog.ProcureCatalogID, DateTime.Now.ToString()), ClientIP = HZ.Fun2.GetClientIP() });
            }
            return state;
        }
        /// <summary>
        /// 检测医疗机构和配送企业是否发生配送关系 发生配送交易禁止删除
        /// </summary>
        /// <param name="HospitalID"></param>
        /// <param name="CompanyID_PS"></param>
        /// <returns></returns>
        public  bool GetBooleanHospitalPurchaseOrder(HZ.DrugTrade.Model.Query.HospitalProcureCatalog where)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetBooleanHospitalPurchaseOrder(where);
        }

        #endregion
        /// <summary>
        /// 查看医疗机构实际价格历史记录
        /// add by Meng 2013-04-22
        /// </summary>
        /// <param name="where"></param>
        /// <param name="currentpage"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalrecord"></param>
        /// <returns></returns>
        public DataTable GetHospitalPriceLog(string where, int currentpage, int pagesize, out int totalrecord)
        {
            BLL_UserSession.CheckSession();
            return DAL.DAL_HospitalProcureCatalog.GetHospitalPriceLog(where, currentpage, pagesize, out totalrecord);
        }
      
    }
}
